package com.chen.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_common.pojo.*;
import com.chen.shopping_common.service.GoodsService;
import com.chen.shopping_common.service.SearchService;
import com.chen.shopping_goods_service.mapper.GoodsImageMapper;
import com.chen.shopping_goods_service.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsImageMapper goodsImageMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    //同步主题（ES）
    private final String SYNC_GOODS_QUEUE = "sync_goods_queue";
    //删除主题（ES）
    private final String DEL_GOODS_QUEUE = "del_goods_queue";
    //同步主题（购物车）
    private final String SYNC_CART_QUEUE = "sync_cart_queue";
    //删除主题（购物车）
    private final String DEL_CART_QUEUE = "del_cart_queue";


    @Override
    public void add(Goods goods) {
        //新增商品
        goodsMapper.insert(goods);
        //新增商品图片
        Long goodsId = goods.getId();
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            image.setGoodsId(goodsId);
            goodsImageMapper.insert(image);
        }
        //新增商品_规格项(向商品_规格项中间表添加数据)
        List<Specification> specifications = goods.getSpecifications();
        //创建容器存储规格项
        List<SpecificationOption> specificationOption = new ArrayList<>();
        for (Specification specification : specifications) {
            specificationOption.addAll(specification.getSpecificationOptions());
        }
        for (SpecificationOption option : specificationOption) {
            goodsMapper.addGoodsSpecification(goodsId,option.getId());
        }

        //将商品同步到ES当中
        GoodsDesc descById = findDescById(goodsId);
        rocketMQTemplate.syncSend(SYNC_GOODS_QUEUE,descById);

    }

    @Override
    public void update(Goods goods) {
        //删除旧商品照片
        Long goodsId = goods.getId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goodsId",goodsId);
        goodsImageMapper.delete(queryWrapper);
        //删除旧商品规格
        goodsMapper.deleteGoodsSpecificationOption(goodsId);
        //新增商品
        goodsMapper.updateById(goods);
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            image.setGoodsId(goodsId);
            goodsImageMapper.insert(image);
        }
        //新增商品_规格项(向商品_规格项中间表添加数据)
        List<Specification> specifications = goods.getSpecifications();
        //创建容器存储规格项
        List<SpecificationOption> specificationOption = new ArrayList<>();
        for (Specification specification : specifications) {
            specificationOption.addAll(specification.getSpecificationOptions());
        }
        for (SpecificationOption option : specificationOption) {
            goodsMapper.addGoodsSpecification(goodsId,option.getId());
        }

        //将商品同步到ES当中
        GoodsDesc descById = findDescById(goodsId);
        rocketMQTemplate.syncSend(SYNC_GOODS_QUEUE,descById);

        //将商品同步到购物车的Redis中
        CartGoods cartGoods = new CartGoods();
        cartGoods.setGoodId(goods.getId());
        cartGoods.setGoodsName(goods.getGoodsName());
        cartGoods.setHeaderPic(goods.getHeaderPic());
        cartGoods.setPrice(goods.getPrice());
        rocketMQTemplate.syncSend(SYNC_CART_QUEUE,cartGoods);
    }

    @Override
    public Goods findById(Long id) {
        Goods goods = goodsMapper.findById(id);
        return goods;
    }

    @Override
    public void putAway(Long id, Boolean isMarketable) {
        goodsMapper.putaway(id,isMarketable);
        if (isMarketable){
            GoodsDesc descById = findDescById(id);
            //在ES中同步上架状态
            rocketMQTemplate.syncSend(SYNC_GOODS_QUEUE,descById);
        }else {
            //在ES中删除该商品
            rocketMQTemplate.syncSend(DEL_GOODS_QUEUE,id);
            //在购物车中同步到商品数据
            rocketMQTemplate.syncSend(DEL_CART_QUEUE,id);
        }
    }

    @Override
    public Page<Goods> findByPage(Goods goods, int page, int size) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (goods!=null && StringUtils.hasText(goods.getGoodsName())){
            queryWrapper.like("goodsName",goods.getGoodsName());
        }
        Page page1 = goodsMapper.selectPage(new Page<Goods>(page, size), queryWrapper);
        return page1;
    }

    @Override
    public List<GoodsDesc> findAll() {
        List<GoodsDesc> goodsDescs = goodsMapper.searchGoodsDescAll();
        return goodsDescs;
    }

    @Override
    public GoodsDesc findDescById(Long id) {
        GoodsDesc goodsDescById = goodsMapper.findGoodsDescById(id);
        return goodsDescById;
    }
}
